﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef IntervalAction_h__
#define IntervalAction_h__

#include "../base/Vector.h"
#include "../utility/SmartPtrDef.h"
#include "Action.h"

#include <vector>
#include <array>
#include <memory>
#include <functional>

namespace SkyEngine2d
{
	/*!
	 \brief 有限时间动作
	 \details
	*/
	class SGE_API ActionInterval
		:public ActionTime 
	{
	public:
		ActionInterval();
		virtual ~ActionInterval();
	
		float getElapsed() { return m_elapsed; }
	
		virtual bool isDone() const override;
		virtual void step(float dt)override;
	
	protected:
		bool  m_firstRunFlag;  //首次运行动画的标记
		float m_elapsed;  //执行这个动画 已经流逝的时间 
	};
	
	
	/*!
	 \brief 序列动作
	 \details
	*/
	class SGE_API ActionSequence
		:public ActionInterval
	{
		FRIEND_SMART_PTR(ActionSequence)
	public:
	
		//! 创建动画的序列
		/*!
		\param const std::vector<ActionTimeSharedPtr> & actions 需要序列执行的动画数组
		\return ActionSequenceSharedPtr  
		*/
		static ActionSequenceSharedPtr create(const std::vector<ActionTimeSharedPtr> & actions);
	
		//! 创建两个动画的序列
		/*!
		\param const ActionTimeSharedPtr & first 第一个动画
		\param const ActionTimeSharedPtr & second 第二个动画
		\return ActionSequenceSharedPtr  
		*/
		static ActionSequenceSharedPtr create(const ActionTimeSharedPtr & first, const ActionTimeSharedPtr & second);
		
		//! 析构
		~ActionSequence();
	
		virtual void update(float t) override;
		virtual void stop()override;
		virtual bool isDone() const override;
	private:
	
		ActionSequence();
	
		bool init(const std::vector<ActionTimeSharedPtr> & actions);
	
		bool init(const ActionTimeSharedPtr & first, const ActionTimeSharedPtr & second);
	private:
		std::array<ActionTimeSharedPtr,2> m_actionArr;  /*!< 保存动画的数组. */
		IActableObject*  m_target;
		float m_split;
		int m_last;
	protected:
		virtual bool startWithTarget(IActableObject* target) override;
		virtual bool setActionTarget(IActableObject* obj) override;
	};
	
	
	
	/*!
	 \brief 匀速位移的动作
	 \details
	*/
	class SGE_API ActionMove
		:public ActionInterval
	{
		FRIEND_SMART_PTR(ActionMove)
	public:
	
		//! 析构
		virtual ~ActionMove();
		//创建一个动画  移动到目标点 
		static ActionMoveSharedPtr createMoveToTargetPos(float pos_x, float pos_y,float duration);
		//创建一个动画  移动一段距离
		static ActionMoveSharedPtr createMoveDistance(float distance_x, float distance_y, float duration);
	
		virtual void update(float ratio)override;
	
	protected:
		bool init(float x, float y, float duration, int flag);
		ActionMove();
	private:
		//开始的坐标
		Vector2 m_startPos;
		//
		int m_flag;
	
		//距离
		Vector2 m_distance;
		
		//结束的位置
		Vector2 m_endPos;
	
		IMovableObject* m_target;
	
	private:
		virtual bool setActionTarget(IActableObject* obj) override;
	
	};
	
	
	/*!
	 \brief 自定义有限时间动作
	 \details
	*/
	class ActionSimple
		:public ActionInterval
	{
		FRIEND_SMART_PTR(ActionSimple)
	public:
		using UPDATE_CALL = std::function<void(IActableObject* target, float ratio)>;
	
		virtual ~ActionSimple();
	
		static ActionSimpleSharedPtr create(const UPDATE_CALL& update_call, float  duration);
	
	protected:
	
		ActionSimple();
		
		bool init(const UPDATE_CALL& update_call, float  duration);
	
		virtual bool setActionTarget(IActableObject* obj) override;
	
	
		virtual void update(float ratio) override;
	
	private:
		IActableObject* m_target;
		UPDATE_CALL m_update;
	};
}
#endif // IntervalAction_h__
